package org.hu.controller;

import org.hu.model.Employee;
import org.hu.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * 登录
 * @author Htrees
 */
@Controller
public class LoginController {
        @Autowired
        EmployeeService employeeService;

    /**
     * 登录页面
     * @param model
     * @return login
     */
    @RequestMapping("/")
        public String login(Model model){
            return "login";
        }

    /**
     * 执行登录操作
     * @param username 用户名
     * @param password 密码
     * @param model
     * @param httpSession
     * @return notifications
     */
        @PostMapping(value = "/doLogin")
        public String dologin(String username, String password, Model model, HttpSession httpSession){
            Employee emp = employeeService.dologin(username, password);
            if(emp == null){
                model.addAttribute("error","用户名或密码输入错误");
                return "forward:/";
            }else if(emp.getStatus()==0){
                model.addAttribute("error","用户待审批");
                return "forward:/";
            }else if(emp.getStatus()==2){
                model.addAttribute("error","用户审批未通过");
                return "forward:/";
            }else{
                httpSession.setAttribute("currentuser",emp);
                return "redirect:/notifications";
            }
        }
        @RequestMapping("/forbbiden")
        String Forbbiden(){
            return "forbbiden";
        }

}
